import pika

def callback(ch, method, properties, body):
    print(f"\n新消息到达 -> {body.decode('utf-8')}")
    print("输入消息: ", end='', flush=True)  # 保持输入提示符位置

def realtime_push():
    # Connection parameters
    credentials = pika.PlainCredentials('XuYixuan', 'Xyx20040422')
    connection = pika.BlockingConnection(pika.ConnectionParameters('82.156.163.134', 5672, '/', credentials))
    channel = connection.channel()

    channel.queue_declare(queue='chat_room', durable=True)

    print("=== 实时消息推送 三号消费者 ===")
    channel.basic_consume(
        queue='chat_room',
        on_message_callback=callback,
        auto_ack=True
    )

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        print("\n停止监听")
        channel.stop_consuming()
        connection.close()

if __name__ == '__main__':
    realtime_push()